package com.itqianfeng.smartmeeting.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itqianfeng.smartmeeting.entity.Room;
import com.itqianfeng.smartmeeting.entity.User;
import com.itqianfeng.smartmeeting.service.RoomService;
import com.itqianfeng.smartmeeting.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;
    @GetMapping("/list")
    public Map list(@RequestParam(name = "page" ,defaultValue = "1") Integer page,
                    @RequestParam(name = "limit",defaultValue = "10") Integer limit){
        try {
            QueryWrapper wrapper=new QueryWrapper<>();
            Page<Room> page1 = new Page<>(page,limit);
            IPage<Room> obj = roomService.page(page1,wrapper);
            return CommonResult.ok("查询成功",obj);
        }catch (Exception e){
            return CommonResult.error("发生异常",-1);
        }
    }
    @PreAuthorize("hasRole('ROLE_USER')")
    @GetMapping("/{id}")
    public Map getById(@PathVariable int id){
        try{
            Room room = roomService.getById(id);
            return CommonResult.ok(room!=null?"查询成功":"会议室不存在",room);
        }catch (Exception e){
            return CommonResult.error("发生异常",-1);
        }
    }
    @PostMapping("/add")
    public Map add(@RequestBody Room room){
        try {
            boolean save = roomService.save(room);
            return CommonResult.ok(save?"添加成功":"添加失败",null);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.error("服务器异常",-1);
        }
    }
    @DeleteMapping ("/delete/{id}")
    public Map deleteById(@PathVariable("id") int id){
        try {
            boolean remove = roomService.removeById(id);
            return CommonResult.ok(remove?"删除成功":"删除失败",null);
        }catch (Exception e){
            return CommonResult.error("发生异常",-1);
        }

    }
    @PutMapping("/update/{id}")
    public Map update(@PathVariable(name = "id") int id, @RequestBody Room room){
        try {
            room.setRoomId(id);
            boolean update = roomService.updateById(room);
            return CommonResult.ok(update?"修改成功":"修改失败",null);
        }catch (Exception e){
            return CommonResult.error("发生异常",-1);
        }
    }
    @DeleteMapping("/delete")
    public Map delete(@RequestBody String[] ids){
        try {
            boolean delete = roomService.removeByIds(Arrays.asList(ids));
            return CommonResult.ok(delete?"删除成功":"删除失败",null);
        }catch (Exception e){
            return CommonResult.error("发生异常",-1);
        }
    }
}
